
import { _decorator, Collider2D, Component, Contact2DType, director, IPhysics2DContact, Node, v2, Vec2, Vec3 } from 'cc';
import { CamreaCtrl } from '../game/CamreraCtrl';
import { Config } from '../utils/Config';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = AiCtrl
 * DateTime = Wed Feb 28 2024 16:54:54 GMT+0800 (中国标准时间)
 * Author = heyongtao
 * FileBasename = AiCtrl.ts
 * FileBasenameNoExtension = AiCtrl
 * URL = db://assets/scripts/ai/AiCtrl.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('AiCtrl')
export class AiCtrl extends Component {
    // [1]
    // dummy = '';

    private initScalePos : Vec3;
    private curPos : Vec3;
    // serializableDummy = 0;
    speed : number = 50;
    //pos : Vec3 = new Vec3(0,0,0);
    player : Node = null;
    private shouldDestroy = false;

    setPlayer(player : Node)
    {
        this.player = player;
    }

    protected onLoad(): void {
        this.initScalePos = this.node.getScale();
        this.curPos = this.node.getPosition();

        //register callback 
        let collider = this.getComponent(Collider2D);
        if(collider)
        {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }

    start () {
        // [3]
    }
    
    checkIsOutofBoundary()
    {
        let cameraPos = CamreaCtrl.GetInstance().getCameraPos();
        let aiPos = this.node.getPosition();
        if((cameraPos.x - Config.GetInstance().GameWinX/2 - Config.GetInstance().MapCornerRect) >= aiPos.x) return true;
        if((cameraPos.x + Config.GetInstance().GameWinX/2 + Config.GetInstance().MapCornerRect) <= aiPos.x) return true;
        if((cameraPos.y - Config.GetInstance().GameWinY/2) >= aiPos.y) return true;
        if((cameraPos.y + Config.GetInstance().GameWinY/2) <= aiPos.y) return true;
        return false;
    }

    onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        //console.log('onBeginContact');
        if(otherCollider.tag == 1) // bullet
        {
            this.shouldDestroy = true;
            otherCollider.node.getComponent("BulletCtrl").setDestroy();
        }
        else if(otherCollider.tag == 2) // skill
        {
            this.shouldDestroy = true;
        }
    }

    // 在摄像机视野范围内存活，离开视野则销毁
    update (deltaTime: number) {
        if(this.checkIsOutofBoundary() || !this.player || this.shouldDestroy)
        {
            this.node.destroy();
            //console.log("ai destory");
            return;
        }
        // //ai move
        let targetPos = this.player.getPosition();
        let curPos = this.node.getPosition();
        const direction = new Vec2(targetPos.x-curPos.x,targetPos.y-curPos.y);
        let normalizedDirection = new Vec2(0,0);
        Vec2.normalize(normalizedDirection,direction);
        const moveDistance = this.speed * deltaTime;
        // vec2 mul
        const scaledDirection = new Vec2(moveDistance*normalizedDirection.x,moveDistance*normalizedDirection.y);
        // vec2 add
        curPos.x += scaledDirection.x;
        curPos.y += scaledDirection.y;
        this.node.setPosition(curPos);
        //console.log("normalizedDirection: ",normalizedDirection);
        // 方向
        let curScale = this.node.getScale();
        let dirX = curPos.x - this.curPos.x > 0 ? 1 : 0;
        curScale.x = dirX ? -this.initScalePos.x : this.initScalePos.x;
        this.node.setScale(curScale);

        //update 
        this.curPos = curPos;
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
